local skill = fk.CreateSkill {
  name = "lvshi__jizhi",
  tags = {"switch"}
}

Fk:loadTranslationTable{
  ["lvshi__jizhi"] = "集智",
  [":lvshi__jizhi"] = "转换技，你可将一张①基本牌②装备牌③【闪电】当【无中生有】使用。出牌阶段开始时，你可卜算X，重置此技能。（X为当前转换项序号）",
  ["#lvshi__jizhi-reset"] = "你可卜算当前序号数，重置此技能",

  ["$lvshi__jizhi1"] = "得上通，智集心。",
  ["$lvshi__jizhi2"] = "集万千才智，制巧具显用。",
  ["$lvshi__jizhi3"] = "以智取胜，四两拨千斤。",
}

local DIY = require "packages/diy_utility/diy_utility"

skill:addEffect("viewas", {
  pattern = "ex_nihilo",
  card_filter = function(self, player, to_select, selected)
    local n = DIY.getSwitchState(player, skill.name)
    if n == 1 then
      return Fk:getCardById(to_select).type == Card.TypeBasic
    elseif n == 2 then
      return Fk:getCardById(to_select).type == Card.TypeEquip
    elseif n == 3 then
      return Fk:getCardById(to_select).trueName == "lightning"
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard("ex_nihilo")
    card:addSubcards(cards)
    card.skillName = skill.name
    return card
  end,
  after_use = function (self, player, use)
    DIY.changeSwitchState(player, skill.name)
  end
})

skill:addEffect(fk.EventPhaseStart, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skill.name) and player.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skill.name, prompt = "#lvshi__jizhi-reset" })
  end,
  on_use = function(self, event, target, player, data)
    local n = DIY.getSwitchState(player, skill.name)
    player.room:askForGuanxing(player, player.room:getNCards(n))
    DIY.setSwitchState(player, skill.name, 1, 3)
  end,
})

skill:addAcquireEffect(function (self, player, is_start)
  DIY.setSwitchState(player, skill.name, 1, 3)
end)

skill:addLoseEffect(function (self, player)
  DIY.removeSwitchSkill(player, skill.name)
end)

return skill